import {Component,ViewChild,OnInit} from 'angular2/core';
import {Page,NavController,Tabs} from 'ionic-angular';
import {VARIABLE} from '../../utils/variable';
import {PubSubService} from '../../services/pub-sub-service';
import {Home} from '../home/home';
import {Find} from '../find/find';
import {Shoppingcart} from '../shoppingcart/shoppingcart';
import {Mine} from '../mine/mine';

import {Category} from '../../models/category';

import {CategoryService}    from '../../services/category';
import {PostService} from '../../services/post';

import {Type} from 'angular2/core';

@Page({
    templateUrl: 'build/pages/tabs/tabs.html',
    directives: [],
    providers: [CategoryService, PostService, PubSubService]
})
export class TabsPage implements OnInit {
    @ViewChild(Tabs)
    tabs: Tabs;

    homeTab: Type = Home;
    findTab: Type = Find;
    shoppingcartTab: Type = Shoppingcart;
    mineTab: Type = Mine;

    categories: Category[] = [];
    errorMessage: any;

    subscription = null;

    constructor(private nav: NavController, private _categoryService: CategoryService) {

    }
    ngOnInit(){
        let me = this;
        me.subscription = PubSubService.getInstance().changeCategory.subscribe(
            category => me.processCategory(category)
        );
    }

    onPageDidEnter(): void {

    }

    processCategory(category){
        let tabIndex = this.tabs.getSelected().index;
        VARIABLE.CURRENT_CATEGORY = category.id;

        if (tabIndex != 1) {
            this.tabs.select(1);
        }
    }
    stopProcessing(){
        this.subscription.unsubscribe();
    }
}
